package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.article.domain.Destination;
import cn.edu.gduf.luowowo.article.service.IDestinationService;
import cn.edu.gduf.luowowo.ask.domain.Question;
import cn.edu.gduf.luowowo.ask.service.IQuestionContentService;
import cn.edu.gduf.luowowo.ask.service.IQuestionService;
import cn.edu.gduf.luowowo.cache.domain.QuestionVo;
import cn.edu.gduf.luowowo.cache.service.IQuestionVoService;
import cn.edu.gduf.luowowo.comment.domain.QuestionComment;
import cn.edu.gduf.luowowo.comment.service.IQuestionCommentService;
import cn.edu.gduf.luowowo.common.query.QueryObject;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.common.util.Consts;
import cn.edu.gduf.luowowo.common.util.DateUtil;
import cn.edu.gduf.luowowo.common.util.RedisKeys;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/ask")
public class AskController {
    @Reference
    private IQuestionService questionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IQuestionContentService questionContentService;
    @Reference
    private IQuestionCommentService questionCommentService;
    @Reference
    private IQuestionVoService questionVoService;

    //添加评论
    @RequestMapping("/add")
    public Object index(Model model) {
        model.addAttribute("dests", destinationService.getDestsByDeep(3));
        return "question/public";
    }

    //跳转添加评论页面
    @RequestMapping("/wenda")
    public Object wenda() {
        return "question/wenda";
    }

    //问答详情页面
    @RequestMapping("/wendaDetail")
    public Object wendaDetail(Model model, Long id) {
        Question question = questionService.selectByPrimaryKey(id);
        question.setContent(questionContentService.selectByPrimaryKey(id));
        model.addAttribute("question", question);
        List<QuestionComment> list = questionCommentService.selectByQuestionId(id);
        model.addAttribute("list", list);
        //问答对象浏览数+1
        questionVoService.viewnumIncrease(id, 1);
        model.addAttribute("questionVo", questionVoService.get(RedisKeys.QUESTION_STATIS_VO.join(id.toString())));
        //金牌排行，暂时使用浏览数
        questionVoService.addHotScore(id, 1, RedisKeys.QUESTION_ZSET_SORT_GOLD.join(DateUtil.getListDateSting(1).get(0)));
        return "question/wendaDetail";
    }

    //保存或修改
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(Question question, String tags, @UserParam UserInfo userInfo) {
        question.setAuthor(userInfo);
        questionService.saveOrUpdate(question, tags);
        return "question/wenda";
    }

    //查询所有
    @RequestMapping("/listAll")
    public Object listAll(Model model, @ModelAttribute("qo") QueryObject qo) {
        qo.setPageSize(Integer.MAX_VALUE);
        PageInfo pageInfo = questionService.listAll(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "question/searchPageTpl";
    }

    //问答评论添加
    @RequestMapping("/commentAdd")
    public Object commentAdd(Model model, QuestionComment questionComment, @UserParam UserInfo userInfo) {
        if (userInfo != null) {
            questionComment.setUserId(userInfo.getId());
            questionComment.setUsername(userInfo.getNickname());
            questionComment.setCity(userInfo.getCity());
            questionComment.setLevel(userInfo.getLevel());
            questionComment.setHeadUrl(userInfo.getHeadImgUrl());
        }
        String id = questionCommentService.saveOrUpdate(questionComment);
        model.addAttribute("c", questionCommentService.findById(id));
        model.addAttribute("floor", questionCommentService.getCount(questionComment.getQuestionId()));
        //问答对象回复数+1
        questionVoService.replynumIncrease(questionComment.getQuestionId(), 1);
        //回答排行
        questionVoService.addHotScore(questionComment.getQuestionId(), 1, RedisKeys.QUESTION_ZSET_SORT_ANSWER.join(DateUtil.getListDateSting(1).get(0)));
        return "question/commentTpl";
    }

    //问答关注
    @RequestMapping("/focusnum")
    @ResponseBody
    public Object focusnum(Long questionId, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "登录后操作");
            result.setCode(102);
            return result;
        }
        //判断该用户是否关注
        boolean flg = questionVoService.focusnum(questionId, userInfo.getId());
        //问答对象关注数
        questionVoService.focusnumIncrease(questionId, flg ? 1 : -1);
        QuestionVo questionVo = questionVoService.get(RedisKeys.QUESTION_STATIS_VO.join(questionId.toString()));
        return new AjaxResult(flg).addData(JSON.toJSON(questionVo));
    }

    @RequestMapping("/checkFocusnumState")
    @ResponseBody
    public Object checkFocusnumState(Long questionId, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return false;
        }
        return questionVoService.checkFocusnumState(questionId, userInfo.getId());
    }

    @RequestMapping("/questionThumbup")
    @ResponseBody
    public Object questionThumbup(Long questionId, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "登录后操作");
            result.setCode(102);
            return result;
        }
        //判断该用户是否顶过
        boolean flg = questionVoService.thumbup(questionId, userInfo.getId());
        if (flg){
            //顶排行
            questionVoService.addHotScore(questionId, 1, RedisKeys.QUESTION_ZSET_SORT_DING.join(DateUtil.getListDateSting(1).get(0)));
        }
        QuestionVo questionVo = questionVoService.get(RedisKeys.QUESTION_STATIS_VO.join(questionId.toString()));
        return new AjaxResult(flg).addData(JSON.toJSON(questionVo));
    }

    @RequestMapping("/getHotTop10")
    public Object getHotTop10(Model model, int type,@ModelAttribute("rank") int rank) {
        List<QuestionVo> list = questionVoService.getHotTop10(type, rank);
        model.addAttribute("list", list);
        return "question/wendaTpl";
    }


    //查询所有
    @RequestMapping("/list")
    @ResponseBody
    public Object list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo pageInfo = questionService.listAll(qo);
        model.addAttribute("pageInfo", pageInfo);
        return pageInfo;
    }
}
